package com.netease.financial.module.activitypage;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.netease.financial.R;
import com.netease.financial.common.util.NfsLog;
import com.netease.financial.module.web.WebViewTabFragment;
import com.netease.financial.ui.activity.BaseAppCompatActivity;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ActivityPageActivity extends BaseAppCompatActivity implements WebViewTabFragment.OnFragmentInteractionListener {

    private static final String TAG = ActivityPageActivity.class.getSimpleName();

    protected static final String INTENT_EXTRA_KEY_PAGE_TITLE = "pageTitle";
    protected static final String INTENT_EXTRA_KEY_PAGE_URL = "pageUrl";

    private TextView mLeftNavigationItem;

    private String mPageTitle;
    private String mPageUrl;
    private ActivityPageFragment mActivityPageFragment;

    @Bind(R.id.btn_share)
    ImageButton btnShare;

    public static Intent getCallingIntent(Context context, String pageTitle, String pageUrl) {
        Intent intent = new Intent(context, ActivityPageActivity.class);
        intent.putExtra(INTENT_EXTRA_KEY_PAGE_TITLE, pageTitle);
        intent.putExtra(INTENT_EXTRA_KEY_PAGE_URL, pageUrl);
        return intent;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_page);
        ButterKnife.bind(this);
        mActivityPageFragment = (ActivityPageFragment) getSupportFragmentManager().findFragmentById(R.id.fragment);
        handleIntent();
        if (!checkArgs()) {
            finish();
            return;
        }
        initialize();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        mActivityPageFragment.onNewIntent(intent);
    }

    private boolean checkArgs() {
        if (TextUtils.isEmpty(mPageUrl)) {
            NfsLog.d(TAG, "pageUrl is empty");
            return false;
        }
        if (TextUtils.isEmpty(mPageTitle)) {
            NfsLog.d(TAG, "pageUrl is empty");
            return false;
        }
        return true;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ButterKnife.unbind(this);
    }

    private void initialize() {
        initActionBar();
    }

    private void handleIntent() {
        Intent intent = getIntent();
        mPageTitle = intent.getStringExtra(INTENT_EXTRA_KEY_PAGE_TITLE);
        mPageUrl = intent.getStringExtra(INTENT_EXTRA_KEY_PAGE_URL);
    }

    private void initActionBar() {
        enableLeftNavigationItem(true);
        setTitle(mPageTitle);
    }

    @Override
    public void setNavigationTitle(final String title) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                setTitle(title);
            }
        });
    }

    @Override
    public void showBottomTabWidget(boolean show) {

    }

    @Override
    public void hideNavBackItem() {
        if (mLeftNavigationItem == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLeftNavigationItem.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public void showNavBackItem() {
        if (mLeftNavigationItem == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mLeftNavigationItem.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void hideOptionMenu() {
        if (btnShare == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btnShare.setVisibility(View.INVISIBLE);
            }
        });
    }

    @Override
    public void showOptionMenu() {
        if (btnShare == null) {
            return;
        }
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                btnShare.setVisibility(View.VISIBLE);
            }
        });
    }

    @Override
    public void setCurrentTab(int position) {
        finish();
        getNavigator().navigateToTab(this, position);
    }

}
